package fans_group

import (
	"encoding/json"
	"longmen/server/config/global"
	fans_messages2 "longmen/server/internal/rpc/tencentim/fans_group/fans_messages"
	"longmen/server/internal/rpc/tencentim/imsdk"
	"longmen/server/internal/rpc/tencentim/messages"
	"longmen/server/internal/rpc/tencentim/utils"
	pb "longmen/server/pkg/protobuf"
)

func (f *fansGroup) beforeMessageProcess() (*pb.CallbackResp, error) {
	//检查用户是否禁言
	code, err := CheckGroupUser(f.req.GroupId, f.user.UserId)
	if err != nil {
		global.Log.Error(err)
		return utils.FailCodeResp(code, err.Error()), nil
	}

	allMsg := f.req.GetMsgBody()
	for _, msg := range allMsg {
		switch msg.MsgType {
		case imsdk.TIM_MSG_TYPE_CUSTOM:
			data := msg.MsgContent.Data
			rawBytes := []byte(data)
			comeMsg := fans_messages2.IncomingMessage{}
			err := json.Unmarshal(rawBytes, &comeMsg)
			if err != nil {
				return utils.FailCodeResp(imsdk.TIM_ERROR_CODE_REJECT, "消息识别错误"), nil
			}
			switch *comeMsg.Type {
			case messages.SayMessageType:
				return fans_messages2.SayMessageHandler(rawBytes, f.req, f.user), nil
			}
		}
	}

	return utils.CallbackOkResp(nil), nil
}
